package com.nulstudio.sppm.Controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.nulstudio.sppm.Annotation.NormalJwt;
import com.nulstudio.sppm.Entities.CreateBasicInfoResponse;
import com.nulstudio.sppm.Entities.SupplierInfo;
import com.nulstudio.sppm.Entities.Warehouse;
import com.nulstudio.sppm.Service.UserInfoService;
import com.nulstudio.sppm.Service.WarehouseService;
import com.nulstudio.sppm.Utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RestController
@RequestMapping("/service/warehouse")
public class WarehouseController {
   @Autowired
    private WarehouseService warehouseService;
   @Autowired
   private UserInfoService userService;
    @NormalJwt
    @PostMapping(value = "/fetchAll",produces = "application/json;charset=UTF-8")
    public String fetchAll() {
        JSONObject json = new JSONObject();
        List<Warehouse> supplierList = warehouseService.getAllWarehouses();
        json.put("code",100);
        json.put("message", "");
        json.put("version",1);
        json.put("result", supplierList);
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value ="/createEntry",produces = "application/json;charset=UTF-8")
    public String createEntry(@RequestBody Map<String,String> map) {
        JSONObject json = new JSONObject();
        String[] properties = new String[]{"name", "type", "address", "status", "authority"};
        for (String property : properties) {
            if (!map.containsKey(property)) {
                json.put("code", 400);
                json.put("message", property + " missed");
                json.put("version", 1);
                json.put("result", "");
                return json.toJSONString();
            }
        }
        Warehouse warehouse = new Warehouse();
        warehouse.setName(map.get(properties[0]));
        warehouse.setType(map.get(properties[1]));
        warehouse.setAddress(map.get(properties[2]));
        warehouse.setStatus(map.get(properties[3]));
        warehouse.setAuthority(map.get(properties[4]));
        final int supplierId = warehouseService.addWarehouse(warehouse);

        json.put("code", 0);
        json.put("message", "");
        json.put("version", 1);
        json.put("result", new CreateBasicInfoResponse(supplierId));
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value ="/modifyEntry",produces = "application/json;charset=UTF-8")
    public String modifyEntry(@RequestBody Warehouse warehouse) {
        JSONObject json = new JSONObject();
        if (warehouse != null) {
            warehouseService.modifyWarehouse(warehouse);
            json.put("code", 0);
            json.put("message", "");
            json.put("version", 1);
            json.put("result","");
            return json.toJSONString();
        }
        json.put("code", 401);
        json.put("message", "no modify request received");
        json.put("version", 1);
        json.put("result","");
        return json.toJSONString();
    }
    @NormalJwt
    @PostMapping(value ="/deleteEntry",produces = "application/json;charset=UTF-8")
    public String deleteEntry(@RequestBody Map<String, Object> map){
        JSONObject json = new JSONObject();
        final String id = (String)map.get("id");
        warehouseService.deleteWarehouse(Integer.parseInt(id));
        json.put("code", 0);
        json.put("message", "");
        json.put("result", "");
        json.put("version", 1);
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value ="/selectWarehouses",produces = "application/json;charset=UTF-8")
    public String selectWarehouses(){
        JSONObject json = new JSONObject();
        List<Warehouse> warehouseList = new ArrayList<>();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        String token = request.getHeader("Authorization");
        final int id = Integer.parseInt(JwtUtils.getUserId(token));
        final String authority = userService.getAuthorityById(id);
        if(authority.equals("manager")){
            warehouseList = warehouseService.getAllWarehouses();
        }else{
            warehouseList = warehouseService.getWarehouseById(id);
        }
        json.put("code", 0);
        json.put("message", "");
        json.put("result", warehouseList);
        json.put("version", 1);
        return json.toJSONString();
    }
}
